package com.bsb.base.common.util;

import java.io.File;

import org.apache.log4j.Logger;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncoderProgressListener;
import it.sauronsoftware.jave.EncodingAttributes;

public class ChangeAudioFormat {
	private static final Logger log = Logger.getLogger(ChangeAudioFormat.class);

	public static void main(String[] args) throws Exception {
		String path1 = "D:\\home\\voice\\夜的钢琴曲5.mp3";
		String path2 = "D:\\home\\voice\\夜的钢琴曲5.aac";
		ChangeAudioListener pro = new ChangeAudioListener();
		changeToAac(path1, path2, pro);
	}

	public static void changeToAac(String sourcePath, String targetPath, ChangeAudioListener listener)
			throws EncoderException {
		File source = new File(sourcePath);
		File target = new File(targetPath);
		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		audio.setBitRate(new Integer(128000));
		audio.setChannels(new Integer(2));
		audio.setSamplingRate(new Integer(44100));
		EncodingAttributes attrs = new EncodingAttributes();

		attrs.setFormat("aac");
		attrs.setAudioAttributes(audio);
		Encoder encoder = new Encoder();
		// String[] audioEncoders = encoder.getAudioEncoders();
		// String[] supportedEncodingFormats =
		// encoder.getSupportedEncodingFormats();
		encoder.encode(source, target, attrs, listener);
	}

	public static void changeToAac(File source, String path, String name, EncoderProgressListener listener)
			throws EncoderException {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		File target = new File(path + "/" + name);
		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		audio.setBitRate(new Integer(128000));
		audio.setChannels(new Integer(2));
		audio.setSamplingRate(new Integer(44100));
		EncodingAttributes attrs = new EncodingAttributes();

		attrs.setFormat("aac");
		attrs.setAudioAttributes(audio);
		Encoder encoder = new Encoder();
		encoder.encode(source, target, attrs, listener);
	}

	public static void changeToAac(File source, String targetPath) throws EncoderException {
		File target = new File(targetPath);
		AudioAttributes audio = new AudioAttributes();
		audio.setCodec("libmp3lame");
		audio.setBitRate(new Integer(128000));
		audio.setChannels(new Integer(2));
		audio.setSamplingRate(new Integer(44100));
		EncodingAttributes attrs = new EncodingAttributes();

		attrs.setFormat("aac");
		attrs.setAudioAttributes(audio);
		Encoder encoder = new Encoder();
		encoder.encode(source, target, attrs);
	}

}